<template>
	<view class="fui-wrap">
		<fui-sticky>
			<view style="background-color: #fff;">
				<fui-segmented-control @click="sectionChange" :current="current" type="text" activeColor="#43afe8" :values="type"></fui-segmented-control>
			</view>
		</fui-sticky>
		<fui-list>
			<fui-list-cell :highlight="false" :padding="['22rpx', '32rpx', '8rpx', '32rpx']" v-for="(item,index) in list" :key="index">
				<view class="item">
					<fui-text :text="item.createtime" color="#2d2d2d" :size="26" block></fui-text>
					<fui-text :text="item.name" color="#2d2d2d" :size="26" block></fui-text>
					<fui-text :text="'停车场：'+item.title" :size="26"></fui-text>
					<fui-text :text="'联系电话：'+item.parking.phone" :size="26" decoration="underline" @click="makecall(item.parking.phone)"></fui-text>
				</view>
				<view class="right">
					<fui-text :text="'￥'+parseNumber(item.total_price)" color="#eb3b18" :size="32" block fontWeight="800" style="margin-right: 20rpx;"></fui-text>
					<fui-button v-if="item.status==1" type="success" @click="download(item.file)" text="查看发票" :size="24"  height="50rpx" width="160rpx" radius="32rpx"></fui-button>
				</view>
			</fui-list-cell>
		</fui-list>
		<fui-empty v-if="list.length===0" :src="`${baseUrl}assets/img/miniapp/empty.png`" title="暂无数据!" :marginTop="200"></fui-empty>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	import {parseNumber} from '@/utils/util.js'
	export default {
		data() {
			return {
				baseUrl:baseUrl,
				type:[
					{id:'invoiced',name:'已开票'},
					{id:'invoicing',name:'开票中'},
				],
				current:0,
				list:[],
				page:1,
				endData:false
			}
		},
		onLoad(e) {
			this.getList();
		},
		onReachBottom() {
			this.getList();
		},
		onPullDownRefresh() {
			this.list=[];
			this.page=1;
			this.endData=false;
			this.getList(function(){
				uni.stopPullDownRefresh();
			});
		},
		methods: {
			...methods,
			parseNumber,
			makecall:function(phone){
				uni.makePhoneCall({
					phoneNumber:phone
				});
			},
			download:function(file){
				let that=this;
				uni.downloadFile({
					url: file,
					success: function(res) {
						if (res.statusCode == 200) {
							uni.openDocument({
								filePath: res.tempFilePath,
								success: (res) => console.log('成功打开文档')
							});
						}
					},
					fail() {
						that.$msg('下载文件失败')
					}
				})
			},
			sectionChange:function(e){
				this.current=e.index;
				this.list=[];
				this.page=1;
				this.endData=false;
				this.getList();
			},
			getList:function(callback=false){
				if(this.endData){
					this.$msg('已经到底了');
					return;
				}
				this.$get('orders/invoice-list',{
					page:this.page,
					type:this.current?0:1
				},true).then(res=>{
					this.list=this.list.concat(res);
					this.page++;
					if(res.length<10){
						this.endData=true;
					}	
					if(callback){
						callback();
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
.item{
	display: flex;
	flex-direction: column;
	height: 160rpx;
	justify-content: space-around;
	width: 500rpx;
}
.right{
	display: flex;
	justify-content: center;
	align-items: center;
}
</style>